Data Types

Validate Table Column Data

Description
This customization demonstrates how to customize the Data Access tier of your application so that the entire application uses a custom class to validate, format, and parse all values for a specific column in a Table. The example uses very simple validation logic, but you can modify the sample code to implement your own business rules.
Variables
Table Name
Select a database table having currency column
Validate Column
Select the name of the currency column to validate
Applies to
DataAccessClass class
Code
 
' Customization for ${Table Name}Access class.
' A custom column class that only accepts MyCustom Currency values.
 Public Class MyCustomCurrencyColumn
    Inherits BaseClasses.Data.CurrencyColumn
    Public Sub New(ByVal col As BaseClasses.Data.CurrencyColumn)
        MyBase.New(col.Number, col.InternalName, col.Name, col.TableDefinition, Nothing, Nothing, Nothing)
        Me.IsIdentity = col.IsIdentity
        Me.DbDataType = col.DbDataType
        Me.DefaultValue = col.DefaultValue
        Me.DatabaseDefaultValue = col.DatabaseDefaultValue
        Me.DisplayFormat = col.DisplayFormat
        Me.DbFormat = col.DbFormat
        Me.IsRequired = col.IsRequired
        Me.IsIndexed = col.IsIndexed
        Me.IsUnique = col.IsUnique
        Me.IsValuesReadOnly = col.IsValuesReadOnly
        Me.IsValuesComputed = col.IsValuesComputed
        ' Me.Precision = col.Precision
        ' Me.Scale = col.Scale
        ' Me.SetConstraintString(col.GetConstraintString())
    End Sub

    ' Overridden to return false for non-valid values.
    Public Overrides Function IsValidValue( _
        ByVal value As BaseClasses.Data.ColumnValue) As Boolean        
        value = Me.ConvertToNativeFormat(value)
        If (Not MyBase.IsValidValue(value)) Then
            Return False
        ElseIf (IsNothing(value) OrElse value.IsNull()) Then
            Return True
        Else
        
            ' Add custom data validation here as shown below.
            ' Return false if value is negative.
            ' If (System.Convert.ToDouble(value.Value) <= 0) Then
            '    Return False
            ' End If        
        End If
        Return True        
    End Function

  ' Overridden to return empty string if not valid value.
  Public Overloads Overrides Function ToDisplayString(ByVal value As ColumnValue, _
      ByVal format As String) As String      
      If (IsValidValue(value)) Then
          Return MyBase.ToDisplayString(value, format)
      Else
          Return ""
      End If      
  End Function
End Class

' Overridden to customize this object's TableDefinition's ColumnList.
Protected Overrides Sub Initialize()
    MyBase.Initialize()

    ' Construct a custom column instance using the current ${Validate Column}.
    Dim newCol As New MyCustomCurrencyColumn(Me.${Validate Column}Column)

    ' Replace the ${Validate Column} with the custom column instance.
    Dim i As Integer 
    i = Me.TableDefinition.ColumnList.IndexOf(Me.${Validate Column}Column)
    Me.TableDefinition.ColumnList.Item(i) = newCol      
  End Sub
     

Terms of Service Privacy Statement